<template>
  <div>
    <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in manegerList" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>

import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, updateDepartments } from '@/api/departments'
export default {
  name: '',
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false
    },
    pid: {
      type: String,
      default: ''
    },
    isEdit: {
      type: Boolean
    },
    row: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: ''
      },
      manegerList: [],
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门管理者不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 200, message: '长度在 1 到 200 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
  },
  watch: {
    isEdit: {
      handler(newValue) {
        if (newValue) {
          this.form = this.row
        }
      },
      immediate: true
    }
  },
  created() {
    this.loadEmployee()
  },
  methods: {
    hSubmit() {
      this.$refs['deptForm'].validate(async(valid) => {
        if (valid) {
          this.form.pid = this.pid
          this.isEdit ? this.doEditDepartment() : this.doAddDepartment()
          // const res = await addDepartments(this.form)
          // // console.log(res, 8888)
          // if (res.code === 10000) {
          //   this.$emit('update:dialogFormVisible', false)
          //   this.form = {
          //     name: '',
          //     code: '',
          //     manager: '',
          //     introduce: '',
          //     pid: ''
          //   }
          //   this.$emit('success')
          // }
          // this.$message({
          //   message: '添加成功!',
          //   type: 'success'
          // })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    async doEditDepartment() {
      const res = await updateDepartments(this.form)
      if (res.code === 10000) {
        this.$emit('update:dialogFormVisible', false)
        this.$emit('success')
      }
    },
    async doAddDepartment() {
      const res = await addDepartments(this.form)
      if (res.code === 10000) {
        this.$emit('update:dialogFormVisible', false)
        this.$emit('success')
      }
    },
    hCancel() {
      this.$emit('update:dialogFormVisible', false)
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: '',
        pid: ''
      }
    },
    async loadEmployee() {
      const res = await getEmployeeSimple()
      // console.log(res, 675)
      this.manegerList = res.data
    }
  }
}
</script>
<style lang='less'  scoped>

</style>
